package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.RolePermission;
import com.example.mall.domain.UserRole;
import com.example.mall.dto.RolePermissionDto;
import com.example.mall.dto.UserRoleDto;
import com.example.mall.mapper.UserRoleMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.RolePermissionQuery;
import com.example.mall.query.UserRoleQuery;
import com.example.mall.service.IUserRoleService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-04-30
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageDTO<UserRoleDto> queryPage(UserRoleQuery userRoleQuery) {
        if (userRoleQuery == null) {
            userRoleQuery = new UserRoleQuery();
        }
        Page<UserRole> page = userRoleQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserRole> cartPage = lambdaQuery()
                .like(userRoleQuery.getUserId()!= null, UserRole::getUserId, userRoleQuery.getUserId())
                .like(userRoleQuery.getUserRoleId()!= null, UserRole::getUserRoleId, userRoleQuery.getUserRoleId())
                .like(userRoleQuery.getRoleId()!= null, UserRole::getRoleId, userRoleQuery.getRoleId())
                .page(page);
        return PageDTO.of(cartPage, UserRoleDto.class);
    }

    @Override
    public UserRole queryUserRoleById(Long userRoleId) {
        return userRoleMapper.selectById(userRoleId);
    }

    @Override
    public List<UserRole> queryUserRoleList() {
        return this.list();
    }

    @Override
    public void insert(UserRoleDto userRoleDto) {
        UserRole userRole = new UserRole();
        CopyUtils.copyProperties(userRoleDto, userRole);
        userRole.setUserRoleId(Long.valueOf(CodeGenerateUtils.generateUniqueId()));
        userRoleMapper.insert(userRole);
    }

    @Override
    public void delete(Long userRoleId) {
        AssertUtil.isNotNull(userRoleId, "用户角色关联表主键不能为空");
        UserRole userRole = userRoleMapper.selectById(userRoleId);
        AssertUtil.isNotNull(userRole, "用户角色关联表不存在");
        userRoleMapper.deleteById(userRoleId);
    }

    @Override
    public void update(UserRoleDto userRoleDto) {
        AssertUtil.isNotNull(userRoleDto.getUserRoleId(), "用户角色关联表主键不能为空");
        UserRole userRole = userRoleMapper.selectById(userRoleDto.getUserRoleId());
        AssertUtil.isNotNull(userRole, "用户角色关联表不存在");
        CopyUtils.copyProperties(userRoleDto, userRole);
        userRoleMapper.updateById(userRole);
    }

    @Override
    public void deleteBatch(List<Long> userRoleIds) {
        AssertUtil.isNotNull(userRoleIds, "用户角色关联表主键不能为空");
        List<UserRole> userRoles = userRoleMapper.selectBatchIds(userRoleIds);
        AssertUtil.isNotNull(userRoles, "用户角色关联表不存在");
        for (UserRole userRole : userRoles) {
            userRoleMapper.deleteById(userRole.getUserRoleId());
        }
    }
}
